package com.zbkj.crmeb.store.service.impl;


import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.CommonPage;
import com.common.PageParamRequest;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zbkj.crmeb.enums.BannerTypeEnum;
import com.zbkj.crmeb.store.dao.StoreBannerDao;
import com.zbkj.crmeb.store.dao.StoreZoneDao;
import com.zbkj.crmeb.store.model.StoreBanner;
import com.zbkj.crmeb.store.model.StoreZone;
import com.zbkj.crmeb.store.service.StoreBannerService;

/**
* @author Mr.Zhang
* @description StoreBannerServiceImpl 接口实现
* @date 2020-05-28 edit by stivepeim 2020-7-4
*/
@Service
public class StoreBannerServiceImpl extends ServiceImpl<StoreBannerDao, StoreBanner> implements StoreBannerService {

    @Resource
    private StoreBannerDao dao;
    @Resource
    private StoreZoneDao storeZoneDao;



    @Override
    public Integer saveStoreBanner(StoreBanner storeBanner) {
        return dao.insert(storeBanner);
    }

    @Override
    public int update(StoreBanner storeBanner) {
        return dao.updateById(storeBanner);
    }

    @Override
    public PageInfo<StoreBanner> getList(StoreBanner storeBanner, PageParamRequest pageParamRequest) {
        Page<StoreBanner> storeBannerPage = PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());
        LambdaQueryWrapper<StoreBanner> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(ObjectUtils.isNotEmpty(storeBanner.getStatus())){
            lambdaQueryWrapper.eq(StoreBanner::getStatus, storeBanner.getStatus());
        }
        lambdaQueryWrapper.eq(StoreBanner::getMerId, storeBanner.getMerId());
        lambdaQueryWrapper.eq(StoreBanner::getIsDel, 0);
        lambdaQueryWrapper.orderByDesc(StoreBanner::getCreateTime);
        List<StoreBanner> list = dao.selectList(lambdaQueryWrapper);
        list.stream().forEach(s ->{
            //2专区
            if(BannerTypeEnum.ZONE.getType() == s.getType()){
                StoreZone storeZone= storeZoneDao.selectById(s.getTypeId());
                if(null!=storeZone){
                    s.setTypeDesc(storeZone.getName());
                }
            }
        });
        return CommonPage.copyPageInfo(storeBannerPage, list);
    }

    @Override
    public List<StoreBanner> getList(StoreBanner storeBanner) {
        LambdaQueryWrapper<StoreBanner> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(StoreBanner::getIsDel, 0);
        lambdaQueryWrapper.eq(StoreBanner::getStatus, 1);
        lambdaQueryWrapper.eq(StoreBanner::getMerId, storeBanner.getMerId());
        lambdaQueryWrapper.orderByDesc(StoreBanner::getSort);
        List<StoreBanner> list = dao.selectList(lambdaQueryWrapper);
        list.stream().forEach(s ->{
            //2专区
            if(BannerTypeEnum.ZONE.getType() == s.getType()){
                StoreZone storeZone= storeZoneDao.selectById(s.getTypeId());
                if(null!=storeZone){
                    s.setTypeDesc(storeZone.getName());
                }
            }
        });
        return list;
    }

    @Override
    public StoreBanner getStoreBanner(Integer id) {
        return dao.selectById(id);
    }
}

